package com.qifenqian.turbot.module.ui.order.orders

import android.content.Context
import android.content.Intent
import android.support.design.widget.BottomSheetDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.blankj.rxbus.RxBus
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.module.adapter.itemdecoration.LinearItemDecoration
import com.qifenqian.turbot.module.adapter.order.OrderDetailAdapter
import com.qifenqian.turbot.module.adapter.order.OrderRefundDialogAdapter
import com.qifenqian.turbot.module.contract.OrderContract
import com.qifenqian.turbot.module.entity.response.order.OrderDetailEntity
import com.qifenqian.turbot.module.presenter.OrderPresenter
import com.qifenqian.turbot.util.Lcat
import kotlinx.android.synthetic.main.activity_order_detail.*
import kotlinx.android.synthetic.main.dialog_dish_refund.*
import kotlinx.android.synthetic.main.item_dialog_dish_refund.view.*

class OrderDetailActivity : BaseActivity<OrderPresenter, OrderContract.View>(), OrderContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, OrderDetailActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_order_detail
    }

    override fun bindPresenter(): OrderPresenter? {
        return OrderPresenter(this, this)
    }

    private var mOrderDishEntityList = mutableListOf<OrderDetailEntity.OrderDishDetail>(
            OrderDetailEntity.OrderDishDetail("a", 1, 100),
            OrderDetailEntity.OrderDishDetail("b", 1, 100),
            OrderDetailEntity.OrderDishDetail("c", 1, 100),
            OrderDetailEntity.OrderDishDetail("d", 1, 100),
            OrderDetailEntity.OrderDishDetail("e", 1, 100),
            OrderDetailEntity.OrderDishDetail("f", 1, 100),
            OrderDetailEntity.OrderDishDetail("g", 1, 100))
    private var mOrderDishEntityList1 = mutableListOf<OrderDetailEntity.OrderDishDetail>()
    private var mOrderDishEntityList2 = mutableListOf<OrderDetailEntity.OrderDishDetail>()

    private var mOrderDetailAdapter: OrderDetailAdapter? = null
    override fun initial() {
        RxBus.getDefault().subscribe(this, RX_REFUND_OK, object : RxBus.Callback<String>() {
            override fun onEvent(t: String?) {
                //退款成功之后该界面的变化
                btn_order_detail_refund.text = t!!
                btn_order_detail_refund.isEnabled = false
                btn_order_detail_refund.setBackgroundColor(resources.getColor(R.color.gray_999999))
            }
        })
        for (i in 0 until mOrderDishEntityList.size) {
            if (i < 5) {
                mOrderDishEntityList1.add(mOrderDishEntityList[i])
            } else {
                mOrderDishEntityList2.add(mOrderDishEntityList[i])
            }
        }
        rv_order_detail_dishes.layoutManager = LinearLayoutManager(this)
        rv_order_detail_dishes.addItemDecoration(LinearItemDecoration(this))
        mOrderDetailAdapter = OrderDetailAdapter(mOrderDishEntityList1)
        rv_order_detail_dishes.adapter = mOrderDetailAdapter
        val footerView = ImageView(this)
        footerView.setImageResource(R.mipmap.ic_verify_default)
        var isFold: Boolean = true
        footerView.setOnClickListener {
            if (isFold) {
                mOrderDetailAdapter!!.addData(mOrderDishEntityList2)
                footerView.setImageResource(R.mipmap.ic_back)
            } else {
                mOrderDetailAdapter!!.removeData(mOrderDishEntityList2)
                footerView.setImageResource(R.mipmap.ic_verify_default)
            }
            isFold = !isFold
        }
        mOrderDetailAdapter!!.addFooterView(footerView)
        tv_order_detail_title.text = "大厅7号"
        tv_order_detail_dishes_money.text = "菜品合计"
        tv_order_detail_discount_money.text = "优惠合计"
        tv_order_detail_balcony_fee.text = "包厢费"
        tv_order_detail_real_money.text = "实际应付"
        tv_order_detail_time.text = "下单时间"
        tv_order_detail_people_count.text = "就餐人数"
        tv_order_detail_order_number.text = "订单编号"
        tv_order_detail_note.text = "备注"

        btn_order_detail_refund.setOnClickListener {
            showRefundDialog()
        }
    }

    private var mRefundDialog: BottomSheetDialog? = null
    private var mOrderRefundDialogAdapter: OrderRefundDialogAdapter? = null
    private var mOrderDishEntityRefundList = mutableListOf<OrderDetailEntity.OrderDishDetail>()
    private var checkedList = mutableListOf<OrderDetailEntity.OrderDishDetail>()

    private fun showRefundDialog() {
        mRefundDialog = BottomSheetDialog(this)
        mRefundDialog!!.setContentView(R.layout.dialog_dish_refund)
        mRefundDialog!!.show()
        mOrderRefundDialogAdapter = OrderRefundDialogAdapter(mOrderDishEntityList)
        mRefundDialog!!.btn_dialog_dish_refund_cancel.setOnClickListener {
            mRefundDialog!!.dismiss()
        }
        mRefundDialog!!.rv_dialog_dish_dishes.layoutManager = LinearLayoutManager(this)
        mRefundDialog!!.rv_dialog_dish_dishes.addItemDecoration(LinearItemDecoration(this, spaceWidth = 1, color = R.color.gray_eeeeee))
        mRefundDialog!!.rv_dialog_dish_dishes.adapter = mOrderRefundDialogAdapter
        val headerView = LayoutInflater.from(this).inflate(R.layout.item_dialog_dish_refund, null)
        val param = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        param.bottomMargin = 16
        headerView.layoutParams = param
        headerView.tv_item_dialog_dish_refund_name.text = "全选"
        headerView.tv_item_dialog_dish_refund_name.setTextColor(resources.getColor(R.color.black_333333))
        headerView.tv_item_dialog_dish_refund_name.textSize = 16f

        mOrderRefundDialogAdapter!!.addHeaderView(headerView)
        var isChecked: Boolean = true
        headerView.cb_item_dialog_dish_refund_check.setOnClickListener { buttonView ->
            if (isChecked) {
                for (orderDishDetail in mOrderDishEntityList) {
                    orderDishDetail.isChecked = true
//                    mOrderRefundDialogAdapter!!.setItemChecked(false)
                }
            } else {
                for (orderDishDetail in mOrderDishEntityList) {
                    orderDishDetail.isChecked = false
//                    mOrderRefundDialogAdapter!!.setItemChecked(true)
                }
            }
            headerView.cb_item_dialog_dish_refund_check.setChecked(isChecked)
            mOrderRefundDialogAdapter!!.notifyDataSetChanged()
            isChecked = !isChecked
        }
        mOrderRefundDialogAdapter!!.setOnCheckedChangeListener(object : OrderRefundDialogAdapter.OnCheckedChangeListener {
            override fun onCheckedChanged(item: OrderDetailEntity.OrderDishDetail) {
                checkedList.clear()
                for (datum in mOrderDishEntityList) {
                    if (datum.isChecked) {
                        checkedList.add(item)
                    } else {
                        checkedList.remove(item)
                    }
                }
                headerView.cb_item_dialog_dish_refund_check.setChecked(checkedList.size == mOrderDishEntityList.size)
            }
        })

        mRefundDialog!!.btn_dialog_dish_refund_ok.setOnClickListener {
            mOrderDishEntityRefundList.clear()
            for (orderDishDetail in mOrderDishEntityList) {
                if (orderDishDetail.isChecked) {
                    mOrderDishEntityRefundList.add(orderDishDetail)
                } else {
                    mOrderDishEntityRefundList.remove(orderDishDetail)
                }
            }
            Lcat.a("取消的菜品", mOrderDishEntityRefundList)
            mRefundDialog!!.dismiss()
            if (mOrderDishEntityRefundList.isNullOrEmpty()) {
                return@setOnClickListener
            }
            OrderRefundResultActivity.startThis(this)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        RxBus.getDefault().unregister(this)
    }

    fun ImageView.setChecked(isChecked: Boolean) {
        if (isChecked) {
            setImageResource(R.mipmap.ic_checked)
        } else {
            setImageResource(R.drawable.shape_circle)
        }
    }
}
